package vg.modules.search.components.searchTreePanel;

import java.awt.Component;
import java.util.Set;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JTree;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeCellRenderer;

import vg.services.data_base_manager.data.skeleton.GraphTreeElement;

public class SearchTreeRenderer extends DefaultTreeCellRenderer{
	private static final long serialVersionUID = 1L;
	//-------------------------------------------------------------------------
	private Set<Integer>array;
	private final Icon iconDelete;
	private final Icon routineIcon;
	private final Icon vertexIcon;
	/**
	 * Constructor.
	 * @param array - list of allowed nodes.
	 */
	public SearchTreeRenderer(final Set<Integer>array) {
		this.array = array;
		this.routineIcon = new ImageIcon("data/resources/textures/workflow.png");
		this.vertexIcon = new ImageIcon("data/resources/textures/vertexBall.png");
		this.iconDelete = new ImageIcon("./data/resources/textures/delete.png");
	}
	/**
	 * @see DefaultTreeCellRenderer
	 */
	public synchronized Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
		TreeCellRenderer delegate = new JTree().getCellRenderer();
		Component returnValue = delegate.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
		if (delegate instanceof JLabel) {
			JLabel renderer = (JLabel) delegate;
			if ((value != null) && (value instanceof GraphTreeElement)) {
				GraphTreeElement node = (GraphTreeElement)value;
				
				if((node.vg_getType() & GraphTreeElement.DEF_VERTEX_TYPE) > 0) {
					if(!node.isLeaf()) {
						renderer.setIcon(this.routineIcon);
					} else {
						if(!array.contains(node.vg_getVertexId())) {
							renderer.setIcon(iconDelete);
						} else {
							renderer.setIcon(vertexIcon);
						}
					}					
				} else if ((node.vg_getType() & GraphTreeElement.DEF_GRAPH_TYPE) > 0){
					renderer.setIcon(routineIcon);
				} else {
					renderer.setIcon(null);
				}
			}
		}
		return returnValue;
	}
	/**
	 * This method sets list of allowed nodes.
	 * @param array - list of allowed nodes.
	 */
	public synchronized void setAllowedList(Set<Integer>array) {
		this.array = array;
	}
}
